package com.hclc.util.tree;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018/9/26.
 */
public class RegionTreeToolUtils {

    private List<RegionTreeBean> rootList; //根节点对象存放到这里

    private List<RegionTreeBean> bodyList; //其他节点存放到这里，可以包含根节点

    public RegionTreeToolUtils(List<RegionTreeBean> rootList, List<RegionTreeBean> bodyList) {
        this.rootList = rootList;
        this.bodyList = bodyList;
    }

    public List<RegionTreeBean> getTree(){   //调用的方法入口
        if(bodyList != null && !bodyList.isEmpty()){
            //声明一个map，用来过滤已操作过的数据
            Map<String,String> map = Maps.newHashMapWithExpectedSize(bodyList.size());
            rootList.forEach(beanTree -> getChild(beanTree,map));//传递根对象和一个空map
            return rootList;
        }
        return null;
    }

    public void getChild(RegionTreeBean beanTree, Map<String,String> map){
        List<RegionTreeBean> childList = Lists.newArrayList();
        bodyList.stream()
                .filter(c -> !map.containsKey(c.getRegionId()))//map内不包含子节点的code
                .filter(c ->c.getPId().equals(beanTree.getRegionId()))//子节点的父id==根节点的code 继续循环
                .forEach(c ->{
                    map.put(c.getRegionId(),c.getPId());//当前节点code和父节点id
                    getChild(c,map);//递归调用
                    childList.add(c);
                });
        beanTree.setChildren(childList);
    }

//    public static void main(String[] args){
//        TreeBean beanTree1 = new TreeBean();
//        beanTree1.setRegionId("540000");
//        beanTree1.setRegionName("西藏省");
//        beanTree1.setPId("100000"); //最高节点
//
//
//
//        TreeBean beanTree2 = new TreeBean();
//        beanTree2.setRegionId("540100");
//        beanTree2.setRegionName("拉萨市");
//        beanTree2.setPId("540000");
//        TreeBean beanTree3 = new TreeBean();
//        beanTree3.setRegionId("540300");
//        beanTree3.setRegionName("昌都市");
//        beanTree3.setPId("540000");
//        TreeBean beanTree4 = new TreeBean();
//        beanTree4.setRegionId("540121");
//        beanTree4.setRegionName("林周县");
//        beanTree4.setPId("540100");
//        TreeBean beanTree5 = new TreeBean();
//        beanTree5.setRegionId("540121206");
//        beanTree5.setRegionName("阿朗乡");
//        beanTree5.setPId("540121");
//
//        TreeBean beanTree6 = new TreeBean();
//        beanTree6.setRegionId("5401212067");
//        beanTree6.setRegionName("瘟疫之源");
//        beanTree6.setPId("540121");
//
//        List<TreeBean> rootList = new ArrayList<>();
//        rootList.add(beanTree1);
//        List<TreeBean> bodyList = new ArrayList<>();
//        bodyList.add(beanTree1);
//        bodyList.add(beanTree2);
//        bodyList.add(beanTree3);
//        bodyList.add(beanTree4);
//        bodyList.add(beanTree5);
//        bodyList.add(beanTree6);
//        TreeToolUtils utils =  new TreeToolUtils(rootList,bodyList);
//        List<TreeBean> result =  utils.getTree();
//        System.out.println(result.get(0));
//        System.out.println(result.get(0).getChildren());
//        System.out.println(result.get(0).getChildren().get(0));
//        System.out.println(result.get(0).getChildren().get(1));
//    }
}
